﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;

using SceneEngine;
using System.Windows.Forms;

namespace SceneEditor
{
    public partial class Editor
    {
        /// <summary>
        /// Prompts the user to create a new scene.
        /// </summary>
        private void tsNewScene_Click(object sender, EventArgs e)
        {
            TextEntryForm textForm = new TextEntryForm();

            textForm.Text = "Choose a name for your scene";
            textForm.lblText.Text = "Name";

            bool suitableNameChosen = false;

            while (!suitableNameChosen)
            {
                DialogResult textResult = textForm.ShowDialog();

                if (textResult != DialogResult.OK)
                {
                    return;
                }

                if (string.IsNullOrEmpty(textForm.tbText.Text) == false)
                {
                    suitableNameChosen = true;
                }
            }

            PromptSaveChanges();

            string directory = EditorGlobals.ContentPath;
            directory = Path.Combine(directory, "Scenes");

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            string scenePath = Path.Combine(directory, textForm.tbText.Text + ".scn");

            if (File.Exists(scenePath))
            {
                DialogResult messageResult = MessageBox.Show("A scene at this location already " +
                                                             "exists, do you want to ovewrite it?",
                                                             "Warning", 
                                                             MessageBoxButtons.YesNo, 
                                                             MessageBoxIcon.Warning);

                if (messageResult != System.Windows.Forms.DialogResult.Yes)
                {
                    return;
                }
            }

            undoActions.Clear();
            redoActions.Clear();

            gizmo.Transforms.Clear();

            scene.Clear();
            scene.Save(scenePath);

            SetUpObjectTree();

            changesMade = false;
            editor.Text = Path.GetFileNameWithoutExtension(scenePath);

            AddSceneToContentProject(scenePath);
        }
        /// <summary>
        /// Prompts the user to open a different scene.
        /// </summary>
        private void tsOpenScene_Click(object sender, EventArgs e)
        {
            SceneForm sceneForm = new SceneForm(EditorGlobals.ContentPath);

            if (sceneForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                PromptSaveChanges();

                undoActions.Clear();
                redoActions.Clear();

                gizmo.Transforms.Clear();

                scene.Load(sceneForm.ScenePath);
                scenePath = sceneForm.ScenePath;

                if (scene.HeightMap != null)
                {
                    scene.HeightMap.HeightMapChanged += new EventHandler(HeightMap_TextureChanged);
                    scene.HeightMap.ColorMapChanged +=new EventHandler(HeightMap_ColorMapChanged);
                    UpdateTerrainTab(true);
                }
                else
                {
                    UpdateTerrainTab(false);
                }

                SetUpObjectTree();

                changesMade = false;
                editor.Text = Path.GetFileNameWithoutExtension(scenePath);
            }
        }

        /// <summary>
        /// Saves the loaded scene.
        /// </summary>
        private void tsSaveScene_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(scenePath))
            {
                tsSaveSceneAs_Click(sender, e);
                return;
            }

            scene.Save(scenePath);
            changesMade = false;

            if (terrainChangesMade && MessageBoxHelper.ShowSaveTerrainChanges() == DialogResult.Yes)
            {
                SaveHeightmap(null, null);
            }

            if (colorMapChangesMade && MessageBoxHelper.ShowSaveColorMapChanges() == DialogResult.Yes)
            {
                SaveColorMap(null, null);
            }

            if (editor.Text.Contains("*"))
            {
                editor.Text = Path.GetFileNameWithoutExtension(scenePath);
            }
        }
        /// <summary>
        /// Saves the loaded scene in a specific directory.
        /// </summary>
        private void tsSaveSceneAs_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
            saveFileDialog.DefaultExt = ".scn";
            saveFileDialog.AddExtension = true;
            saveFileDialog.OverwritePrompt = true;
            saveFileDialog.SupportMultiDottedExtensions = false;

            if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                scene.Save(saveFileDialog.FileName);
                scenePath = saveFileDialog.FileName;
            }

            if (terrainChangesMade && MessageBoxHelper.ShowSaveTerrainChanges() == DialogResult.Yes)
            {
                SaveHeightmap(null, null);
            }

            if (colorMapChangesMade && MessageBoxHelper.ShowSaveColorMapChanges() == DialogResult.Yes)
            {
                SaveColorMap(null, null);
            }

            changesMade = false;

            if (editor.Text.Contains("*"))
                editor.Text = Path.GetFileNameWithoutExtension(scenePath);
        }

        /// <summary>
        /// Prompts the user to select a project to link to.
        /// </summary>
        private void tsSwitchProject_Click(object sender, EventArgs e)
        {
            if (splash.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            PromptSaveChanges();

            undoActions.Clear();
            redoActions.Clear();

            InitializeEditor();
        }
    }
}
